<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>集合结构</title>
    集合里面的元素无序，且不能重复
    意味着集合里面的元素不能通过下标来获取，不能重复意味着一个元素在集合内只可能存在一份
</head>
<body>
    <script>
        class Set {
            constructor() {
                this.items = {}
            }
            add (value) {
                if (this.has(value)) { // 判断是否已存在该元素，如果存在则返回添加失败
                    return false
                }
                this.items[value] = value
                return true
            }
            has(value) {
                return this.items.hasOwnProperty(value)
            }
            delete(value) {
                if (this.has(value)) {
                    return false
                }
                delete this.items[value]
                return true
            }
            clear() {
                this.items = {}
            }
            size() {
                return Object.keys(this.items).length
            }
            values() {
                return Object.values(this.items)
            }
            // 并集操作
            union(otherSet) {
                let unionSet = new Set()
                let values = this.values()
                forValues(values)
                values = otherSet.values()
                forValues(values)
                
                function forValues(values) {
                    for (let i = 0; i < values.length; i++) {
                        const item = values[i];
                        unionSet.add(item)
                    }
                }
                return unionSet
            }
            // 交集操作
            intersection(otherSet) {
                let intersectionSet = new Set()
                let values = this.values()
                for (let i = 0; i < values.length; i++) {
                    const ele = values[i];
                    if (otherSet.has(ele)) {
                        intersectionSet.add(ele)
                    }
                }
                return intersectionSet
            }
            // 差集操作
            difference(otherSet) {
                let differenceSet = new Set()
                let values = this.values()
                for (let i = 0; i < values.length; i++) {
                    const ele = values[i];
                    if (!otherSet.has(ele)) {
                        differenceSet.add(ele)
                    }
                }
                return differenceSet
            }
            // 子集操作
            isSub(otherSet) {
                let values = this.values()
                for (let i = 0; i < values.length; i++) {
                    const ele = values[i];
                    if (!otherSet.has(ele)) {
                        return false
                    }
                }
                return true
            }
        }
        let set = new Set()
        set.add('abc')
        set.add('bca')
        set.add('cab')
        let othSet = new Set()
        othSet.add('1')
        othSet.add('2')
        othSet.add('3')
        othSet.add('abc')
        console.log(`交集：`)
        console.log(set.union(othSet).values())
        console.log(`并集：`)
        console.log(set.intersection(othSet).values())
        console.log(`差集：`)
        console.log(set.difference(othSet).values())
        console.log(`子集：`)
        console.log(set.isSub(othSet))

        
    </script>
</body>
</html>